//
//  PPFeatureGuideViewController.m
//  pickpic
//
//  Created by Jinxiao on 10/23/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPFeatureGuideViewController.h"
#import "PPLoginViewController.h"
#import "PPLoginNavigationController.h"

#import "PPLoginAndRegisterViewController.h"


#import "PPFeatureView.h"

#define IWNewfeatureImageCount 4
@import MediaPlayer;
@import AVFoundation;

@interface PPFeatureGuideViewController ()<UIScrollViewDelegate>
//@property (nonatomic, weak) UIPageControl *pageControl;
@property (readwrite, nonatomic, strong) MPMoviePlayerViewController *player;
@property (nonatomic, strong) UIImageView *firstFPSImage;
@property (nonatomic, strong) UIButton *skipButton;
@property(nonatomic,strong)   NSTimer * timer;
@property (nonatomic,strong)  UIScrollView *scrollView;

@property (nonatomic,strong)  PPFeatureView *featureView;
@end

@implementation PPFeatureGuideViewController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
///**
// *  添加pageControl
// */
//- (void)setupPageControl
//{
//    // 1.添加
//    UIPageControl *pageControl = [[UIPageControl alloc] init];
//    pageControl.numberOfPages = IWNewfeatureImageCount;
//    CGFloat centerX = self.view.frame.size.width * 0.5;
//    CGFloat centerY = self.view.frame.size.height - 149 + 20;
//    pageControl.center = CGPointMake(centerX, centerY);
//    pageControl.bounds = CGRectMake(0, 0, 100, 80);
//    pageControl.userInteractionEnabled = NO;
//    [self.view addSubview:pageControl];
//    self.pageControl = pageControl;
//    
//    // 2.设置圆点的颜色
//    pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
//    pageControl.pageIndicatorTintColor = [UIColor groupTableViewBackgroundColor];
//}

/**
 *  添加UISrollView
 */
//- (void)setupScrollView
//{
//    UIScrollView *scrollView = [[UIScrollView alloc] init];
//    scrollView.frame = self.view.bounds;
//    scrollView.delegate = self;
//    [self.view addSubview:scrollView];
//    
//    // 2.添加图片
//    CGFloat imageW = scrollView.frame.size.width;
//    CGFloat imageH = scrollView.frame.size.height;
//    for (int index = 0; index<IWNewfeatureImageCount; index++) {
//        UIImageView *imageView = [[UIImageView alloc] init];
//        
//        // 设置图片
//        //        NSString *name = nil;
//        //        if (fourInch) { // 4inch
//        //            name = [NSString stringWithFormat:@"new_feature_%d-568h", index + 1];
//        //        } else {
//        NSString * name = [NSString stringWithFormat:@"start_%d.jpg", index + 1];
//        //        }
//        imageView.image = [UIImage imageNamed:name];
//        
//        // 设置frame
//        CGFloat imageX = index * imageW;
//        imageView.frame = CGRectMake(imageX, 0, imageW, imageH);
//        
//        [scrollView addSubview:imageView];
//        
//        // 在最后一个图片上面添加按钮
//        if (index == IWNewfeatureImageCount - 1) {
////            [self setupLastImageView:imageView];
////                imageView.userInteractionEnabled = YES;
////            UISwipeGestureRecognizer *swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(skipButtonDidPress:)];
//////             UITapGestureRecognizer *swipeRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(skipButtonDidPress:)];
////            
////            [imageView addGestureRecognizer:swipeRec];
////            
//        }
//    }
//    
//    // 3.设置滚动的内容尺寸
//    scrollView.contentSize = CGSizeMake(imageW * IWNewfeatureImageCount/* + 5*/, 0);
//    scrollView.showsHorizontalScrollIndicator = NO;
//    scrollView.pagingEnabled = YES;
//    scrollView.bounces = NO;
//    self.scrollView = scrollView;
//}
/**
 *  只要UIScrollView滚动了,就会调用
 *
 */
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    CGFloat offsetX = scrollView.contentOffset.x;
//    double pageDouble = offsetX / scrollView.frame.size.width;
//    int pageInt = (int)(pageDouble + 0.5);
//    self.pageControl.currentPage = pageInt;
//    
////    if (offsetX > self.view.width*IWNewfeatureImageCount-self.view.width) {
////        [self skipButtonDidPress];
////    }
//}
//-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
//{
//    [self.timer invalidate];//取消计时器
//    self.timer = nil;//避免野指针
//}
- (void)addLoginBtn{
    UIButton *startButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [startButton setBackgroundImage:[UIImage imageNamed:@"start_login"] forState:UIControlStateNormal];
    [startButton setBackgroundImage:[UIImage imageNamed:@"start_login"] forState:UIControlStateHighlighted];
    [startButton setTitle:@"登录与注册" forState:UIControlStateNormal];
    startButton.titleLabel.font = PPFONTSIZE(18.f);
    [startButton setTitleColor:BLACK forState:UIControlStateNormal];
    CGFloat centerX = self.view.width * 0.5;
    CGFloat centerY = self.view.height - 122 + 64;
    
    CGFloat leftRightMargin = 26;
    CGFloat buttonH = 48;
    if (IS_IPHONE_6) {
        centerY = self.view.height -buttonH/2 - 35;
        leftRightMargin = 47;
    }else if (IS_IPHONE_6_PLUS){
        centerY = self.view.height -buttonH/2 - 44;
        leftRightMargin = 52;
        
    }
    
    startButton.center = CGPointMake(centerX, centerY);
  
    startButton.bounds = (CGRect){CGPointZero,CGSizeMake(SCREEN_WIDTH-leftRightMargin*2, buttonH)};
//    startButton.backgroundColor = BLUE;
    [startButton addTarget:self action:@selector(skipButtonDidPress) forControlEvents:UIControlEventTouchUpInside];
//    startButton.layer.borderColor = [UIColor blackColor].CGColor;//设置边框颜色
//    startButton.layer.borderWidth = 0.5f;//设置边框颜色
    [self.view addSubview:startButton];
}
- (void)viewDidLoad {
    [super viewDidLoad];

    
    PPFeatureView *featureView = [[PPFeatureView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    NSMutableArray *imgs = [NSMutableArray array];
    for (int index = 0; index<IWNewfeatureImageCount; index++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        NSString * name = [NSString stringWithFormat:@"start_%d", index + 1];
        imageView.image = [UIImage imageNamed:name];
        
        [imgs addObject:imageView];
    }
    featureView.images = @[[UIImage imageNamed:@"start_1"],[UIImage imageNamed:@"start_2"],[UIImage imageNamed:@"start_3"]];
    
    [self.view addSubview:featureView];
    
//    // 1.添加UISrollView
//    [self setupScrollView];
//    
//    // 2.添加pageControl
//    [self setupPageControl];
//    
    //3.添加登录注册
    [self addLoginBtn];
//
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:4.0f target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
    
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerDidEnd) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(prepareToPlay) name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resumeMoviePlayer) name:UIApplicationWillEnterForegroundNotification object:nil];
// 播放视频
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"guide" ofType:@"mov"];
//    _player = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
//    [_player.moviePlayer prepareToPlay];
//    _player.view.frame = [UIScreen mainScreen].bounds;
//    [self.view addSubview:_player.view];

//    _firstFPSImage = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
//    _firstFPSImage.image = [UIImage imageNamed:@"guide_video_first_fps.jpg"];
//    [_firstFPSImage setContentMode:UIViewContentModeScaleAspectFill];
//    [_player.view addSubview:_firstFPSImage];

//    _player.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
//    _player.moviePlayer.controlStyle = MPMovieControlStyleNone;
//    _player.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
//    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
//    NSError *error = nil;
//    [audioSession setCategory:AVAudioSessionCategoryAmbient error:&error];
//    [audioSession setActive:YES error:&error];
//
//    _skipButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 15, 50, 30)];
//    [_skipButton addTarget:self action:@selector(skipButtonDidPress) forControlEvents:UIControlEventTouchUpInside];
//    _skipButton.titleLabel.font = [UIFont systemFontOfSize:12];
//    [_skipButton setTitleColor:[UIColor colorWithRGBHex:0x4d4d4d] forState:UIControlStateNormal];
//    [_skipButton setTitle:@"跳过" forState:UIControlStateNormal];
//    _skipButton.backgroundColor = [UIColor redColor];
//    [self.view addSubview:_skipButton];
//    _skipButton.hidden = YES;
    
    

}

- (void)moviePlayerDidEnd {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    [self doLogin];
}

- (void)prepareToPlay {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification object:nil];

    _firstFPSImage.hidden = YES;
    [_firstFPSImage removeFromSuperview];
    _skipButton.hidden = NO;
}

- (void)resumeMoviePlayer {
    [_player.moviePlayer play];
}

- (void)skipButtonDidPress {
    
        [self doLogin];

}
- (void)changeImage{
    //获得当先scrollView滚动到的点（俗称偏移量）
    CGFloat offSetX = self.scrollView.contentOffset.x;//获取当前滚动视图的contentOffSet的x值
    
    //让scrollView向右滚动一个屏幕宽的距离
    offSetX += self.scrollView.bounds.size.width;
    
    [self.scrollView setContentOffset:CGPointMake(offSetX, 0) animated:YES];//开始
    
}

- (void)doLogin {
//    PPLoginViewController *loginViewController = [[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateViewControllerWithIdentifier:@"PPLoginViewController"];
//    [self.timer invalidate];//取消计时器
//    self.timer = nil;//避免野指针
    
     [self.featureView stopTimer];
    
     [[UIApplication sharedApplication] setStatusBarHidden:NO];
    
    PPLoginAndRegisterViewController *loginViewController = [[PPLoginAndRegisterViewController alloc] init];
    
//    PPLoginNavigationController *navigationController = [[PPLoginNavigationController alloc] initWithRootViewController:loginViewController];
    
    
    
          PPBaseNavigationViewController *navigationController = [[PPBaseNavigationViewController alloc] initWithRootViewController:loginViewController];
    
    [[[UIApplication sharedApplication] delegate] window].rootViewController = navigationController;

    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"AppGuided"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end
